// Name: David Andersen

using UnityEngine;
using System.Collections;

public class EnemyMove : MonoBehaviour {
	
	public int moveSpeed;
	public Transform bomb;
	Vector3 computerDirection = Vector3.left;
	public int reloadTime;
	public GameObject controller;
	
	private bool canShoot = false;
	private int reloading = 0;

	// Use this for initialization
	void Start () {
		controller = GameObject.Find("Controller");
		moveSpeed = controller.GetComponent<Controller_Script>().enemySpeed;
		reloadTime = controller.GetComponent<Controller_Script>().reloadTime;
	}
	
	// Update is called once per frame
	void Update () {
		
		Vector3 newPosition = Vector3.zero;
		newPosition = computerDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = newPosition + transform.position;
		
		if(newPosition.x > 98)
		{
			newPosition.x = 98;
			computerDirection.x *= -1;
		}
		else if(newPosition.x < -98)
		{
			newPosition.x = -98;
			computerDirection.x *= -1;
		}
		transform.position = newPosition;
		
		if(canShoot == true)
		{
			Transform newBomb = Instantiate(bomb, transform.position, transform.rotation) as Transform;
			reloading = 0;
			canShoot = false;
		}
		else
		{
			reloading ++;
			if(reloading >= reloadTime)
			{
				canShoot = true;
			}
		}
	}
}
